package org.neuedu.ssmp.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import org.neuedu.ssmp.crm.entity.*;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.mapper.CustomerMapper;
import org.neuedu.ssmp.crm.service.*;
import org.neuedu.ssmp.crm.util.PinyinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户前端控制器
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IAreaService areaService;
    @Autowired
    private ICustomerLevelService levelService;
    @Autowired
    private ICustomerContactService contactService;
    @Autowired
    private IRelationshipService relationshipService;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * @return返回客户信息
     */
    @GetMapping("customerInfo")
    public IPage<Customer> list(int pageIndex, int pageSize, @RequestParam(required = false) String name, @RequestParam(required = false) String managerName) {
        return customerService.conditionPage(pageIndex, pageSize, name, managerName);
    }

    /**
     * @return修改客户信息
     */
    @PutMapping("customerInfo/edit")
    public ResponseEntity editCustomerInfo(@RequestBody Customer customer) {
        return customerService.editCustomerInfo(customer);
    }

    /**
     * @return 获取联系人信息
     */
    @GetMapping("customerInfo/contact")
    public IPage<CustomerContact> list(int pageIndex, int pageSize, int customerId) {
        return contactService.contactPage(pageIndex, pageSize, customerId);
    }

    /**
     *
     * @param contact 联系人
     * @return 新建联系人成功与否
     */
    @PostMapping("customerInfo/contact/add")
    public ResponseEntity addContact(@RequestBody CustomerContact contact) {
        return contactService.add(contact);
    }

    /**
     *
     * @param id 联系人id
     * @return 删除联系人成功与否
     */
    @DeleteMapping("customerInfo/contact/del")
    public ResponseEntity delContact(int id) {
        return contactService.del(id);
    }

    /**
     *
     * @param contact 联系人
     * @return 更新联系人成功与否
     */
    @PutMapping("customerInfo/contact/edit")
    public ResponseEntity editContact(@RequestBody CustomerContact contact) {
        return contactService.edit(contact);
    }

    /**
     *
     * @param pageIndex 页码
     * @param pageSize  每页显示个数
     * @param customerId 客户id
     * @return  交往记录信息
     */
    @GetMapping("customerInfo/relationship")
    public IPage<Relationship> relationshipList(int pageIndex, int pageSize, int customerId) {
        return relationshipService.relationshipPage(pageIndex, pageSize, customerId);
    }

    /**
     *
     * @param relationship 交往记录
     * @return  新建记录成功信息
     */
    @PostMapping("customerInfo/relationship/add")
    public ResponseEntity addRelationship(@RequestBody Relationship relationship) {
        return relationshipService.add(relationship);
    }

    /**
     *
     * @param id 交往记录id
     * @return 删除记录成功与否
     */
    @DeleteMapping("customerInfo/relationship/del")
    public ResponseEntity delRelationship(int id) {
        return relationshipService.del(id);
    }

    /**
     *
     * @param relationship 交往记录
     * @return 修改交往记录信息
     */
    @PutMapping("customerInfo/relationship/edit")
    public ResponseEntity editRelationship(@RequestBody Relationship relationship) {
        return relationshipService.edit(relationship);
    }
    /**
     *
     * @param pageIndex 页码
     * @param pageSize  每页显示个数
     * @param customerId 客户id
     * @return  历史订单信息
     */
    @GetMapping("customerInfo/orders")
    public IPage<Orders> ordersPage(int pageIndex, int pageSize, int customerId){
        return ordersService.ordersPage(pageIndex,pageSize,customerId);
    }

    /**
     *
     * @param pageIndex 页码
     * @param pageSize  每页显示个数
     * @param orderId 订单id
     * @return  订单明细信息
     */
    @GetMapping("customerInfo/orders/orderDetails")
    public IPage<OrderList> orderListPage(int pageIndex, int pageSize, int orderId){
        return ordersService.orderDetailPage(pageIndex,pageSize,orderId);
    }

    /**
     * @return返回地区信息，用于下拉框
     */
    @GetMapping("areaInfo")
    public List<Area> areaInfo() {
        return areaService.list();
    }

    /**
     * @return返回客户经理信息，用于下拉框
     */
    @GetMapping("managerInfo")
    public List<SysUser> managerInfo() {
        return customerService.managerList();
    }

    /**
     * @return返回客户等级信息，用于下拉框
     */
    @GetMapping("levelInfo")
    public List<CustomerLevel> levelInfo() {
        return levelService.list();
    }


    @PostMapping("insert")
    @Transactional
    public ResponseEntity inserts(@RequestBody Customer customer){
        customer.setCode(PinyinUtil.toPinyin(customer.getName()).toUpperCase());
        if (customerService.save(customer)) {
            Customer one = new LambdaQueryChainWrapper<>(customerMapper).eq(Customer::getName, customer.getName()).one();
            CustomerContact contact = CustomerContact.builder().customerId(one.getId()).name(customer.getManagerName()).telephone(customer.getPhone()).build();
            contactService.save(contact);
            return ResponseEntity.ok("成功");
        }
        return ResponseEntity.error("失败");
    }
}
